package com.zzxx.userManagement.service.impl;

import com.zzxx.userManagement.dao.UserInfoDao;
import com.zzxx.userManagement.dao.impl.UserInfoDaoImpl;
import com.zzxx.userManagement.domain.UserInfo;
import com.zzxx.userManagement.service.UserInfoService;
import com.zzxx.userManagement.utils.PageBean;

import java.util.List;
import java.util.Map;

public class UserInfoServiceImpl implements UserInfoService {
    private UserInfoDao userInfoDao = new UserInfoDaoImpl();
    @Override
    public List<UserInfo> query() {
        return userInfoDao.findAll();
    }

    @Override
    public void deleteById(Integer id) {
        userInfoDao.deleteById(id);
    }

    @Override
    public UserInfo findUserInfoById(Integer id) {
        return userInfoDao.findById(id);
    }

    @Override
    public void updateUserInfo(UserInfo userInfo) {
        userInfoDao.update(userInfo);
    }

    @Override
    public void addUserInfo(UserInfo userInfo) {
        userInfoDao.add(userInfo);
    }

    @Override
    public PageBean<UserInfo> queryPage(String _currentPage, String _pageSize, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);
        // List
        int start = (currentPage-1)*pageSize;
        List<UserInfo> list = userInfoDao.findByPage(start, pageSize, condition);
        // totalCount
        int totalCount = userInfoDao.findCount(condition);
        // totalPage
//        int totalPage = totalCount/pageSize == 0 ? totalCount/pageSize:totalCount/pageSize+1;
        int totalPage = (totalCount + pageSize - 1) / pageSize;
        // 封装PageBean对象
        PageBean<UserInfo> pageBean = new PageBean<>();
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(currentPage);
        pageBean.setList(list);
        pageBean.setTotalCount(totalCount);
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }

    @Override
    public void deleteSelected(String[] ids) {
        for (String _id : ids) {
            userInfoDao.deleteById(Integer.parseInt(_id));
        }
    }
}
